<?php
    include_once("../action/checkAuthenticationAction.php");
    include_once("../service/userService.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Dashboard</title>
	<?php include("import-css.php");?>	
    <style>
        #sortable1, #sortable2 { list-style-type: none; margin: 5px 0 0 0; padding: 0 0 2.5em; float: left; margin-right: 10px; border:1px solid black; min-height:20px; min-width:160px; border-radius:4px;}
        #sortable1 li, #sortable2 li {cursor:pointer; margin: 0 5px 5px 5px; padding: 5px; font-size: 1.2em; width: 120px; min-width:140px;}
    </style>
</head>
<body>
	<?php include("header.php");?>
    
    <div class="container-fluid">
		<div class="row-fluid">
            <!--Menu-->
            <?php include("menu.php");?>
            <?php
                $action = isset($_GET["action"]) ? $_GET["action"] : 8;
                if($action == 9){
                    $user_id = $_GET["id"];
                    $edit_user = getStaffUser($user_id);
                }
            ?>
            <!--Content-->
            <?php
                if(!authUtil(array("edit staff","create staff","view staff","delete staff"),$perm_array,true)){
                    echo "<script>window.location.href='../admin/index.php';</script>";
                }
            ?>
            <div class="box span10 offset2">
                <div class="box-content">
                    <form class="form-horizontal" method="POST" action="<?php echo (($action == 8) ? "../action/newStaffAction.php" : "../action/editStaffAction.php"); ?>" id="new_user_form">
                        <div class="control-group">
                            <label for="userTitle" class="control-label">Tài khoản nhân viên</label>
                            <div class="controls">
                                <input <?php echo ($action == 8)?"required":"disabled"; ?> type="text" value="<?php echo ($action == 9 ?  $edit_user["USER_USERNAME"] : ""); ?>" id="userTitle" name="userTitle" class="input-xlarge focused"/>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="userPassword" class="control-label">Password</label>
                            <?php 
                                if($action == 8){
                            ?>
                            <div class="controls">
                                <input required type="password" value="" id="userPassword" name="userPassword" class="input-xlarge focused"/>
                            </div>
                            <?php
                                }else if($action == 9){
                            ?>
                                <div class="controls">
                                    <a href="../action/resetStaffPassword.php?id=<?php echo $edit_user["USER_ID"]; ?>">Reset Password</a>
                                </div>
                            <?php
                                }
                            ?>
                        </div>
                        <div class="control-group">
                            <label for="userFullname" class="control-label">Họ tên</label>
                            <div class="controls">
                                <input required type="text" value="<?php echo ($action == 9 ?  $edit_user["USER_FULLNAME"] : ""); ?>" id="userFullname" name="userFullname" class="input-xlarge focused"/>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="userGender" class="control-label">Giới tính</label>
                            <div class="controls">
                                <select id="userGender" name="userGender">
                                    <option value="1" <?php echo ($action == 9 && $edit_user["USER_GENDER"]==1 ?  "selected" : ""); ?>>Nam</option>
                                    <option value="0" <?php echo ($action == 9 && $edit_user["USER_GENDER"]==0 ?  "selected" : ""); ?>>Nữ</option>
                                </select>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="userEmail" class="control-label">Email</label>
                            <div class="controls">
                                <input required type="text" value="<?php echo ($action == 9 ?  $edit_user["USER_EMAIL"] : ""); ?>" id="userEmail" name="userEmail" class="input-xlarge focused"/>
                            </div>
                        </div>
						<!--GROUPS-->
                        <div class="control-group">
                            <label class="control-label">Nhóm</label>
                            <div class="controls" id="groupControlGroup">
                                <ul id="sortable1" class="connectedSortable">
                                        <?php
                                            if($action == 8){
                                                $groups = getAllUserGroup();
                                            }else if($action == 9){
                                                $groups = getAllAvailableGroup($edit_user["USER_ID"]);
                                            }
                                            while($group = mysql_fetch_array($groups)){
                                                echo "<li class='ui-state-default' value='".$group["ROLE_ID"]."'>".$group["ROLE_NAME"];
                                                echo "<input type='hidden' name='selectedPrivilegess[]' value='".$group["ROLE_ID"]."'/>";
                                                echo "</li>";
                                            }
                                        ?>
                                </ul>
                                <div style="float:left;margin-right:5px;">===></div>
                                <ul id="sortable2" name="sortable2[]" class="connectedSortable">
                                    <?php
                                        if($action == 9){
                                            $groups = getAllGroupOfUser($edit_user["USER_ID"]);
                                        }
                                        while($group = mysql_fetch_array($groups)){
                                            echo "<li class='ui-state-default'";
                                            echo " value='".$group["ROLE_ID"]."'>".$group["ROLE_NAME"];
                                            echo "<input type='hidden' name='selectedPrivilegess[]' value='".$group["ROLE_ID"]."'/>";
                                            echo "</li>";
                                        }
                                    ?>                                    
                                </ul>
                            </div>
                        </div>
                        <center>
                        <button type="submit" class="btn btn-primary"><?php echo (($action == 8) ? "Tạo mới" : "Cập nhật"); ?></button>
                        </center>
                        <input type="hidden" name="edit_user_id" id="edit_user_id" value="<?php echo isset($edit_user["USER_ID"])?$edit_user["USER_ID"]:""; ?>"/>
                        
                    </form>
                </div>
                
            </div><!--/span-->
        </div>
    </div>

    <?php include("footer.php");?>
		
	</div><!--/.fluid-container-->

	<?php include("import-js.php");?>
    <script>
    $(document).ready(function(){
        $("#new_user_form").submit(function(e){
            e.preventDefault();
            var form = this;
			//Do not submit attributes and options in left-side list
            privileges1 = $("#sortable1").find("input");
            privileges1.each(function(){
                $(this).attr("name","");
            });
            form.submit();
        });
    });
    $(function() {
		$( "#sortable1, #sortable2" ).sortable({
            connectWith: ".connectedSortable"
        });
    });
    
    </script>
</body>
</html>

